//
//  BlockListViewController.m
//  MagicBox
//
//  Created by dllo on 16/8/19.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "BlockListViewController.h"
#import "EMSDK.h"
#import "BaseTableView.h"
@interface BlockListViewController ()<UITableViewDelegate, UITableViewDataSource>

@property(nonatomic, strong) NSMutableArray *blockListArray;
@property(nonatomic, strong) BaseTableView *tableView;

@end

@implementation BlockListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"黑名单";
    
    self.tableView = [[BaseTableView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT) style:UITableViewStyleGrouped];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.view addSubview:_tableView];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _blockListArray.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    cell.textLabel.text = [self.blockListArray objectAtIndex:indexPath.row];
    
    return cell;
}

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

// 删除方法
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}


// 当是删除模式的时候的点击方法
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        NSString *userName = [self.blockListArray objectAtIndex:indexPath.row];
      
        [[EMClient sharedClient].contactManager asyncRemoveUserFromBlackList:userName success:^{
            
            UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"%@已被移出黑名单啦", userName] message:nil preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"我知道了" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            
           
            [alertVc addAction:action];
            [self presentViewController:alertVc animated:YES completion:^{
               
                
            }];
            
            
            EMError *error = nil;
            self.blockListArray = [NSMutableArray arrayWithArray:[[EMClient sharedClient].contactManager getBlackListFromServerWithError:&error]];
        
            [self.tableView reloadData];
            
            
        } failure:^(EMError *aError) {
            
        }];
        
        
        
        
    }
    
    

    
}



-(void)viewWillAppear:(BOOL)animated{
    EMError *error = nil;
    
    self.blockListArray = [NSMutableArray arrayWithArray:[[EMClient sharedClient].contactManager getBlackListFromServerWithError:&error]];
    
    if (!error) {
        return;
    }
    self.blockListArray = [NSMutableArray arrayWithArray:[[EMClient sharedClient].contactManager getBlackListFromDB]];
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
